package com.bpf.base.security;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import com.bpf.base.bean.system.TSysMenuForm;
import com.bpf.base.bean.system.TSysSetForm;
import com.bpf.base.bean.system.TSysUserForm;
import com.bpf.base.common.Constants;
import com.bpf.base.model.system.OperateLog;
import com.bpf.base.model.system.TSysMenu;
import com.bpf.base.model.system.TSysSet;
import com.bpf.base.model.system.TSysUser;
import com.bpf.base.service.system.OperateLogService;
import com.bpf.base.service.system.Role2MenuService;
import com.bpf.base.service.system.TSysMenuService;
import com.bpf.base.service.system.TSysRoleService;
import com.bpf.base.service.system.TSysSetService;
import com.bpf.base.service.system.TSysUserService;
import com.bpf.base.util.Collections3;
import com.bpf.base.util.MapUtil;
import com.bpf.base.util.SpringSecurityUtil;

public class CustomAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

	@Autowired
	private TSysUserService userService;
	
	@Autowired
	private TSysSetService tSysSetService;

	@Autowired
	private TSysRoleService roleService;

	@Autowired
	private TSysMenuService tSysMenuService;
 
	@Autowired
	private Role2MenuService role2MenuService;

	@Autowired
	private OperateLogService operateLogService;
	
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
		// 请求的IP
		String ip = "";
		if (request.getHeader("x-forwarded-for") == null) {
			ip = request.getRemoteAddr();
		} else {
			ip = request.getHeader("x-forwarded-for");
		}
		// *========数据库日志=========*//
		OperateLog operateLog = new OperateLog();
		operateLog.setOperateDescription("登录系统");
		operateLog.setOperateType(Constants.OperateLogType.OPERATE_ACTION);
		operateLog.setOperateIp(ip);
		operateLog.setOperateRequestUrl(request.getRequestURL().toString());
		operateLog.setOperateParams(request.getQueryString());
		operateLog.setOperateCreateBy(SpringSecurityUtil.getPrincipal().getUsername());
		operateLog.setOperateCreateTime(new Date());
		// 保存数据库
		operateLogService.insertSelective(operateLog);

		TSysUserForm userForm=new TSysUserForm();
		userForm.setUsername(SpringSecurityUtil.getPrincipal().getUsername());
		TSysUser loginUser = userService.getAllTSysUser(MapUtil.toMap(userForm)).get(0);
		
		//登陆管理员门户
		request.getSession(true).setAttribute("loginUser", loginUser);
		//request.getSession(true).setAttribute("infrasList",infrasService.selectByCustomer(loginUser.getCustomerId()));
		
		// 登录管理员关联的菜单,所有的系统菜单
		List<TSysMenu> allMenu = tSysMenuService.selectByRoleId(loginUser.getRoleId());
		//拿到角色对应的工况菜单
		if (Collections3.isEmpty(allMenu) == false) {
			
			TSysMenuForm topform = new TSysMenuForm();
			topform.setSystemId(loginUser.getSystemId());
			topform.setMenuParentId(-1);
			TSysMenu topmenu = tSysMenuService.selectList(MapUtil.toMap(topform)).get(0);
			
			// 1默认为根节点
			// top菜单
			List<TSysMenu> topMenuList = new ArrayList<TSysMenu>();
			for (TSysMenu menu : allMenu) {
				if (topmenu.getMenuId().equals(menu.getMenuParentId())) {
					topMenuList.add(menu);
				}
			}
			
			
			//上边二级菜单
			List<TSysMenu> tempTop2MenuList = new ArrayList<TSysMenu>();
			if (!Collections3.isEmpty(topMenuList)) {
				List<TSysMenu> topTMenuList;
				for (TSysMenu topMenu : topMenuList) {
					topTMenuList = new ArrayList<TSysMenu>();
					for (TSysMenu menu : allMenu) {
						if (menu !=null && menu.getMenuParentId().equals(topMenu.getMenuId())) {
							topTMenuList.add(menu);
							tempTop2MenuList.add(menu);
						}
					}
					topMenu.setChildren(topTMenuList);
				}
			}
			
			// 左边的菜单
			List<TSysMenu> tempLeftMenuList = new ArrayList<TSysMenu>();
			// 为top菜单添加左边子菜单
			if (!Collections3.isEmpty(tempTop2MenuList)) {
				List<TSysMenu> leftMenuList;
				for (TSysMenu topMenu : tempTop2MenuList) {
					leftMenuList = new ArrayList<TSysMenu>();
					for (TSysMenu menu : allMenu) {
						if (menu !=null && menu.getMenuParentId().equals(topMenu.getMenuId())) {
							leftMenuList.add(menu);
							tempLeftMenuList.add(menu);
						}
					}
					topMenu.setChildren(leftMenuList);
				}
			}
			// 左边的菜单
			List<TSysMenu> proLeftMenuList = new ArrayList<TSysMenu>();
			// 为工况top菜单添加左边子菜单
			
			// 左边的菜单
			List<TSysMenu> proLeftMenuList1 = new ArrayList<TSysMenu>();
			// 为地图top菜单添加左边子菜单
			
			// 左边的菜单
			List<TSysMenu> proLeftMenuList2 = new ArrayList<TSysMenu>();
			// 为地图top菜单添加左边子菜单
			
			// 为left的菜单的添加子菜单
			if (!Collections3.isEmpty(tempLeftMenuList)) {
				List<TSysMenu> leftChildMenuList;
				for (TSysMenu leftMenu : tempLeftMenuList) {
					leftChildMenuList = new ArrayList<TSysMenu>();
					for (TSysMenu menu : allMenu) {
						if (menu !=null && menu.getMenuParentId().equals(leftMenu.getMenuId())) {
							leftChildMenuList.add(menu);
						}
					}
					leftMenu.setChildren(leftChildMenuList);
				}
			}
			// 为工况left的菜单的添加子菜单

			// 为工况left的菜单的添加子菜单
			request.getSession(true).setAttribute("menuList", topMenuList);

		}
		super.onAuthenticationSuccess(request, response, authentication);
	}
	
	List<TSysMenu> getMenuList(List<TSysMenu> allMenu){
		return allMenu;
	}
	
}
